/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Aptech.DAL;

import Aptech.DALInterface.BuiltsInterface;
import Aptech.DAL.MgBuilts;
import HibernateMappingPoJo.Builts;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author nkhoan
 */
public class MgBuilts implements BuiltsInterface {
   private SessionFactory sessFact;
    private Session sess;
    private Transaction tr;

    @Override
    public List getAllBuilt() {
                List list = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            list = sess.createCriteria(Builts.class).list();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }

        return list;
    }

    @Override
    public void addBuilt(Builts builts){
         try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();
            sess.save(builts);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {

            System.out.println(he.getMessage());

        } finally {
            sess.close();

        }
    }

    @Override
    public void deleteBuilt(String id){

        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            String sql = "delete Builts where ID = '" + id + "'";
            sess.createSQLQuery(sql).executeUpdate();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }
    }

    @Override
    public void editBuilt(Builts builts){
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();

            sess.update(builts);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();

        }
    }
    
    @Override
    public Builts getBuiltsByID(String id) {
        Builts built = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            Criteria criteria = sess.createCriteria(Builts.class).add(Restrictions.eq("id", id));
            built = (Builts) criteria.uniqueResult();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }
        return built;
    }
}
